
const MyPromiseTools = {

  all: function(origin) {
    if (!Array.isArray(origin))
      throw new Error('arguments must be a array');

    return new Promise((resolve, reject) => {
      let _len = origin.length,
          _cur = 0,
          target = [];
          
      for (let i = 0; i < _len; i++) {
        Promise.resolve(origin[i]).then(value => {
          _cur++;
          target[i] = value;
          if (_cur === _len)
            return resolve(target);
        }, reason => {
          return reject(reason);
        })
      }
    })
  },

  race: function(origin) {
    if (!Array.isArray(origin))
      throw new Error('arguments must be a array');
      
    return new Promise((resolve, reject) => {
      for (let i = 0; i < origin.length; i++ ) {
        origin[i].then(resolve, reject);
      }
    })
  }

}

module.exports = MyPromiseTools;